package com.yfproject.business;

import java.util.List;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.yfproject.models.Labels;



@Repository
@CacheConfig(cacheNames="label")
public interface LabelsRepository extends CrudRepository<Labels, Integer>{
	
	/**
	 * 根据parentId和status查询、分页标签信息
	 * @param parentId
	 * @param status
	 * @param pageable
	 * @return
	 */
	@Cacheable(key="'findAndPageByParentIdAndStatus'+#p0+#p1+#p2.getPageNumber()+#p2.getPageSize()")
	Page<Labels> findByParentIdAndStatus(Long parentId,int status,Pageable pageable);
	
    /**
     * 根据ParentId查询标签并分页
     * @param parentId
     * @param pageable
     * @return
     */
	@Cacheable(key="'findAndPageByParentId'+#p0+#p1.getPageNumber()+#p1.getPageSize()")
	Page<Labels> findByParentId(Long parentId,Pageable pageable);
	
	/**
	 * 根据level查询标签并分页
	 * @param level
	 * @param pageable
	 * @return
	 */
	@Cacheable(key="'findByLevel'+#p0+#p1.getPageNumber()+#p1.getPageSize()")
	Page<Labels> findByLevel(int level,Pageable pageable);
	
	/**
	 * 根据标签名模糊查询所有标签并分页
	 * @param name
	 * @param pageable
	 * @return
	 */
	@Cacheable(key="'findByName'+#p0+#p1.getPageNumber()+#p1.getPageSize()")
	@Query("select a from Labels a where a.name like %?1%")
	Page<Labels> findByName(String name,Pageable pageable);
	
	/**
	 * 根据标签名模糊查询可用标签并分页
	 * @param name
	 * @param status
	 * @param pageable
	 * @return
	 */
	@Cacheable(key="'findByNameAndStatus'+#p0+#p1+#p2.getPageNumber()+#p2.getPageSize()")
	@Query("select a from Labels a where a.name like %?1% and a.status=?2")
	Page<Labels> findByNameAndStatus(String name, Integer status,Pageable pageable);
	
	@Cacheable(key="'findById'+#p0")
	List<Labels> findById(Long id);

	@Cacheable(key="'findByParentId'+#p0")
	List<Labels> findByParentId(Long id);
	
	@Cacheable(key="'findByLevel'+#p0")
	List<Labels> findByLevel(int level);
	
	List<Labels> findByLevelGreaterThan(int level);

	@Cacheable(key="'findByParentIdAndStatus'+#p0+#p1")
	List<Labels> findByParentIdAndStatus(Long id, int status);
	
	@Cacheable(key="'findByName'+#p0")
	@Query("select a from Labels a where a.name like %?1%")
	List<Labels> findByName(String name);
	
	@Cacheable(key="'findByNameAndStatus'+#p0+#p1")
	@Query("select a from Labels a where a.name like %?1% and a.status = ?2")
	List<Labels> findByNameAndStatus(String name, Integer status);

}
